<title>我的发票</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>我的发票</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-myinvoice-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">票据类型</label>
                    <div class="layui-input-block">
                        <select name="billType" id="ID-select-myinvoice-billtype">
                            <option value="">不限</option>
                            <option value="1">电子发票（普通发票）</option>
                            <option value="2">电子发票（增值税专用发票）</option>
                            <option value="3">电子发票（航空运输电子客票行程单）</option>
                            <option value="4">电子发票（铁路电子客票）</option>
                            <option value="5">增值税电子普通发票</option>
                            <option value="6">增值税电子专用发票</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">发票号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="invoiceNumber" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">开票日期</label>
                    <div class="layui-inline" id="ID-laydate-rangeLinked-issueDate">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-start-date-myinvoice-list-issuedate" name="issueDateStart" class="layui-input" placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-end-date-myinvoice-list-issuedate" name="issueDateEnd" class="layui-input" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">上传时间</label>
                    <div class="layui-inline" id="ID-laydate-rangeLinked-uploadTime">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-start-date-myinvoice-list-uploadtime" name="uploadTimeStart" class="layui-input" placeholder="开始时间">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-end-date-myinvoice-list-uploadtime" name="uploadTimeEnd" class="layui-input" placeholder="结束时间">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">文件名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="fileName" placeholder="请输入" autocomplete="off" class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="comment" placeholder="请输入" autocomplete="off" class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-myinvoice-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 查询
                    </button>
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-on="myinvoiceformlistclear">
                        <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 刷新
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-myinvoice-manage" lay-filter="LAY-myinvoice-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="batchUploadInvoice">批量上传发票</button>
                    <button class="layui-btn layui-btn-sm" lay-event="typeIn">手工录入发票</button>
                    <button class="layui-btn layui-btn-sm" lay-event="viewResult">查看识别结果</button>
                </div>
            </script>
            <script type="text/html" id="table-myinvoice-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
            <script type="text/html" id="table-useradmin-templet-status">
                {{#  if(d.isEnabled == 'Y'){ }}
                <span style="color: green">启用</span>
                {{#  }else{ }}
                <span style="color: red">禁用</span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-templet-role">
                {{#  if(d.role == 'ADMIN'){ }}
                <span style="">系统管理员</span>
                {{#  }else if(d.role == 'TENANT'){ }}
                <span style="">租户</span>
                {{#  }else{ }}
                <span style="">未知用户</span>
                {{#  } }}
            </script>
            <script type="text/html" id="ID-table-myinvoice-templet-billTypeName">
                <span>{{ d.billTypeName }}</span>
                {{# if(d.billType==1 || d.billType==5){ }}
                <span style="color:#16b777; border: 1px solid; padding: 0px 1px;">普</span>
                {{# }else if(d.billType==2 || d.billType==6){ }}
                <span style="color:#1e9fff; border: 1px solid; padding: 0px 1px;">专</span>
                {{# }else if(d.billType==3){ }}
                <span style="color: #a233c6; border: 1px solid; padding: 0px 1px;">机</span>
                {{# }else if(d.billType==4){ }}
                <span style="color: #ff5722; border: 1px solid; padding: 0px 1px;">铁</span>
                {{#  } }}
            </script>
            <script type="text/html" id="ID-table-myinvoice-templet-invoiceNumber">
                <a class="layui-table-link billtong-myinvoice-table-invoicenumber">{{= d.invoiceNumber }}</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use('myinvoice', layui.factory('myinvoice')).use(['admin', 'myinvoice', 'table', 'dropdown', 'util'], function () {
        var $ = layui.$
            , admin = layui.admin
            , setter = layui.setter
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , laydate = layui.laydate
            , util = layui.util
            , dropdown = layui.dropdown;

        form.render(null, 'layadmin-myinvoice-formlist');

        //处理自定义事件
        util.on({
            myinvoiceformlistclear: function () {
                console.log(this); // 当前触发事件的 DOM 元素

                $('#ID-select-myinvoice-billtype').val('');
                $(':input[name=invoiceNumber]').val('');
                $(':input[name=issueDateStart]').val('');
                $(':input[name=issueDateEnd]').val('');
                $(':input[name=uploadTimeStart]').val('');
                $(':input[name=uploadTimeEnd]').val('');
                $(':input[name=fileName]').val('');
                $(':input[name=comment]').val('');
                form.render($('#ID-select-myinvoice-billtype'));

                //执行重载
                table.reload('LAY-myinvoice-manage', {
                    where: {}
                });
            }
        });

        // 日期范围 - 左右面板联动选择模式
        //开票日期
        laydate.render({
            elem: '#ID-laydate-rangeLinked-issueDate',
            range: ['#ID-laydate-start-date-myinvoice-list-issuedate', '#ID-laydate-end-date-myinvoice-list-issuedate'],
            rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  2.8+ 新增
        });

        // 日期范围 - 左右面板联动选择模式
        //上传时间
        laydate.render({
            elem: '#ID-laydate-rangeLinked-uploadTime',
            range: ['#ID-laydate-start-date-myinvoice-list-uploadtime', '#ID-laydate-end-date-myinvoice-list-uploadtime'],
            rangeLinked: true,
            type: 'datetime',
            fullPanel: true
        });

        //开启搜索
        form.on('submit(LAY-myinvoice-front-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-myinvoice-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-myinvoice-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-myinvoice-manage');
                        layer.msg('已删除');
                    });
                });
            }
            , batchupload: function () {
                admin.popup({
                    title: '发票批量上传'
                    , area: ['500px', '450px']
                    , id: 'LAY-popup-myinvoice-batchupload'
                    , success: function (layero, index) {
                        view(this.id).render('myinvoice/uploadform').done(function () {
                            form.render(null, 'layuiadmin-form-myinvoiceadmin');

                            //开启提交
                            form.on('submit(LAY-user-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                $.ajax({
                                    url: setter.baseConfig.apiHost + '/api/user/add' //实际使用请改成服务端真实接口
                                    , type: 'post'
                                    , contentType: "application/json"
                                    , data: JSON.stringify(field)
                                    , beforeSend: function (xhr) {
                                        xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
                                    }
                                    , success: function (res) {
                                        if (res.errorCode != '00000') {
                                            layer.msg(res.errorMessage, {
                                                offset: '15px'
                                                , icon: 2
                                                , time: 3000
                                            });

                                            return false;
                                        }

                                        //登入成功的提示与跳转
                                        layer.msg('添加成功', {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 3000
                                        });

                                        layui.table.reload('LAY-myinvoice-manage'); //重载表格
                                        layer.close(index); //执行关闭
                                    }
                                });
                            });
                        });
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>